/**
 * zmn黑名单IPjs
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author wangjie
 * @since 2019/06/13 13:58
 */
let $;
let whitelistType;
let title;
layui.define(['index', 'table', 'laypage'], function (exports) {
    $ = layui.$;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    whitelistType = json.whitelistType;
    title = json.title;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-140',
        done:function () {
            //添加按钮
            let layout = [{
                title: '导出',
                layEvent: 'CUSTOM_EXPORT',
                icon: 'layui-icon-export',
                display: json.outputExcel
            }];
            zmnUtil.toobarRightAddButton(layout);
        }
    });
    laypage.render(zmnPage.laypageOptions(json));

    //头工具栏事件
    table.on('toolbar(zmn-table)', function (obj) {

        if (obj.event === 'add') {
            let url = "/whitelist/add_whitelist.action?whitelistType=" + whitelistType;
            zmnLayUtil.openIframeCustom(layer, url, {
                title: "新增" + title + "白名单",
                area: ["40%", "23%"]
            });
        } else if (obj.event === 'CUSTOM_EXPORT') {
            let index = layer.load(1, {
                shade: [0.5, '#DBDBDB']
            });
            zmnUtil.downloadLimitByConfig("/whitelist/outputExcel.action?" + $("#zmn-form").serialize(), "白名单报表.xlsx", function () {
                layer.close(index);
            });
        }
    });

    //监听行工具事件
    table.on('tool(zmn-table)', function (obj) {
        let data = obj.data;
        alert(data)
    });

    //对外暴露的接口
    exports('whitelist_list', {});
});

function deleteWhitelist(val, numStr) {

    let index;

    layer.confirm('确认删除' + title + '："' + numStr + '" 吗？', {
        skin: 'demo-class',
        btn: ['确认', '取消']
    }, function () {
        $.ajax({
            async: true,
            type: "POST", url: "/whitelist/delete_whitelist.action",
            data: {whitelistId: val, whitelistType: whitelistType},
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                location.reload();
                if (json.status === 200) {
                    layer.msg(json.message, {"icon": 1});
                } else {
                    layer.msg(json.message, {"icon": 2});
                }
                layer.close(index);

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    });
}



